AxoCalculator implements several different programming languages. This document describes its implementation of the C language. True C is a large, complex language. AxoCalculator implements a simplified subset of C. If you are familiar with C, check the following carefully to find out what you can and can't do in AxoCalculator's C. In addition to omitting many features, some of the strict requirements of C have been relaxed to make programming AxoCalculator easier. For example, C is case sensitive, but AxoCalculator C is not. C requires that all variables be declared and that all statements end with a semicolon. These requirements make it laborious to perform simple calculations. For example, to set two variables, a and b, to 10 and 20 respectively, add them together, then output the result you would need to type the following using true C :
float a, b;
a = 10;
b = 20;
printf (a+b);
In contrast, AxoCalculator's C language does not require variables to be declared, ignores semicolons and outputs the result of any expression which lacks an assignment operator. This means you only have to type :
a = 10
b = 20
a + b
AxoCalculator can execute both of the above programs. First choose "Settings…" under the "Calculator" menu, and make sure the programming language is set to C. To execute one or more lines of code, select (highlight) them using the mouse, then press the "enter" key.
Strings
Character strings are enclosed in double quotes (e.g. "This is a string"). To include a double quote, tab or return in a character string, use
\" for a quote,
\t for a tab
\n or \r for a return.
String variables can be created in two ways. The simplest is to assign a string to a previously unused variable name. For example,
aString = "Hello world"
As an alternative, use the "NewString" procedure.
NewString (bString)
bString="abcde"
Strings can be appended to one another using the "Concat" function.
NewString (cString)
cString = concat (aString, " ", bString)
Individual characters in a string can be accessed and manipulated. A string variable behaves like a 255 element array. Each element is one character. Accessing an element returns the ASCII code of that character. The length of the string is stored in the zero indexed element.
AxoCalculator programs can interact with the user via standard dialogs. This is done using two built in procedure calls, "Alert" and "PoseDialog". These procedures are described in the "Built in Procedures" document. "Alert" is used for simple messages or for returning results. "PoseDialog" is used for requesting one or more numerical values. An example program follows :
PoseDialog ("\n Calculate the volume and surface area of a sphere",
& "Radius of sphere ",radius)
theSA = 4 * pi * radius ^ 2
theVolume = (4 / 3) * pi * radius ^ 3
Alert ("The volume and surface area of a \n sphere with radius ", radius,
& " are : \n Volume = ",theVolume, "\n Surface area = ",theSA)
Conditional Branch
AxoCalculator supports the standard "if else" statement for conditional branching. A program demonstrating this statement follows :
/* This program finds the square root of a number entered by the user */
PoseDialog ("\n Find the square root of a number", "Enter the number", a)
if (a < 0)
Alert ("Can't calculate the square root of a negative number : ",a)
else {
b = sqrt (a)
Alert ("\n The square root of ",a,"is ",b)
}
Note : because the "else" condition required two lines to be executed,
these lines are enclosed in curly brackets.
AxoCalculator's C language relaxes the use of semi-colons after every statement. This makes writing simple programs easier, but can lead to ambiguities with "if else" statements. Do not use the following styles,
A) "if else " all on the same line
if (condition1) then action1 else action2
Instead use,
if (condition1) action1
else action2
or,
if (condition1)
action1
else
action2
B) "else if' " extending over more than one line
if (condition1)
action1
else if (condition2)
action2
else
action3
Instead use,
if (condition1)
action1
else {
if (condition2)
action2
else
action3
}
Loop Commands
AxoCalculator supports several commands for executing a group of statements multiple times. These loop commands are "For", "While" and "Do While". Executable programs demonstrating each of these commands follow.
Note : To interrupt a running program (for example to escape from
an infinite loop) press the "esc" key, or the Cmd-period
key combination.
• The "For" command
For ( i = 1 ; i <= 5 ; i++ ) {
a = i * 5
b = sin (a)
printf ("sin ( ",a,") = ",b)
}
• The "While" command
a = 6
While (a > 0) {
a--
b = exp (a)
printf ("exp ( ",a,") = ",b)
}
• The "Do While" command
a = 0
b = 0
Do {
a += 1
b += 2
c = b ^ a
printf (b,"^ ",a," = ",c)
}
While (a <= 5)
These loop commands can also be used to execute a single line expression multiple times. In this situation, curly brackets are not needed. For example :
For ( i = 1 ; i <= 20 ; i++ ) write (i)
Variable Declaration
Variables created without being declared (as in the above examples) are always "float" (i.e. floating point). To work with Integer, Boolean or Array variables, they must be declared in standard C fashion. For example :
int i,j
boolean b
float r
float anArr[50]
string aStr
Each variable name is preceded by the type of the variable. For array variables, the size of the array is also specified. Only float Arrays are supported.
Arrays can be created anywhere in a program using the "NewArray" procedure,
NewArray (anArray, arraySize)
Variables may be declared as either global (available to all programs and procedures) or local (available only to the currently active procedure). Global variables are preserved until they are explicitly unloaded. Local variables, which are declared within a function, are automatically unloaded when the function finishes executing. Local variables are declared immediately after a "Function" or "Program" declaration line (see below), after the first "{", and before the first statement. Global variables are declared before any "Function" or "Program" declarations. Examples of both local and global variable declaration can be found in the "Example Programs" document.
Procedures, Functions and Programs
AxoCalculator's programming language can be extended by loading user defined Procedures, Functions and Programs. These all have the same basic form :
• a declaration line which includes a name and lists any parameters
• an open curly bracket.
• an optional local variable declaration section
• a program section ending with a close curly bracket.
• an optional "return" statement.
Parameters are always passed by value.
Passing parameter by reference is not supported.
Programs can not have any parameters.
A Procedures, Function or Program can be loaded by selecting it's text, then pressing "enter" or choosing "Load" under the "Calculator" menu. The only difference is that "Load" will not attempt to execute any lines of code outside the program declaration, and may therefore produce a clearer error message.
To run a Procedures, Function or Program, type in its name followed by any parameters. A Function may be executed as part of a numerical expression. Programs and Functions may call other Programs and Functions.
Declaring a Procedure
Procedures in C are simply functions that return "Void". Here is an example of how to declare a simple Procedure with a single parameter. This procedure has no local variable declaration section. To load it, select the following 4 lines, and press "enter". To run it, type "CountTo(10)" then "enter".
void CountTo (Number)
{
For ( j = 1 ; j <= Number ; j++ ) printf (j)
}
Declaring a Function
Here is an example of how do declare a simple Function with a single parameter. A function returns a numerical result, and the type of the result (float, int or boolean) is specified before the function's name. Load this example function as above.
float Factorial (n)
{
float f
int j
f = 1
For ( j = 1 ; j <= n ; j++ ) f = f * j
return f
}
Here are two examples of how to use the "Factorial" function.
Factorial(10)
printf ("The natural log of factorial 5 = ", ln( Factorial(5)) )
One practical use for functions is for unit conversion. Here is an example function which converts inches to centimeters.
float inchToCm (inch)
{
return inch * 2.54
}
inchTocm(5)
Note: A general unit conversion function is provided in the document
"Unit Conversions" in the "AxoCalculator AutoLoad" folder.
Adding a Program to the Calculator Menu
Here is an example of how to declare a simple program. Note that a program has no parameter list, and its name is followed by an optional character string. If present, this string is appended to the calculator menu when the program is loaded. If the second last character of the string is a slash ( / ) then the last character becomes a command key equivalent for running the program. Load the program as above. To run it, type "CountDown" then "enter", or select "Count Down to Lift Off" from the "Calculator" menu.
program CountDown "Count Down to Lift Off/9"
{
printf
printf ("Prepare for count down.")
FlushOutput
/* Pause */
for (k = 1 ; k <= 50 ; k++) a = exp(2.0)
/* Start the count down */
j = 10
While (j >= 0)
{
if (j == 3)
printf ("Ignition")
if (j != 0)
printf (j)
else
printf ("Lift Off !!")
FlushOutput
Beep
j = j - 1
/* Slow down the count */
for (k = 1 ; k <= 20 ; k++) a = exp(2.0)
}
}
Auto-loading a Program
A useful program can be automatically loaded every time AxoCalculator is started up. To auto-load one or more programs, simply place them in the folder "AxoCalculator AutoLoad". This folder must be located in the same folder as the AxoCalculator program.